package com.elink.enginerring.file;

import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.elink.enginerring.common.ErrorType;
import com.elink.enginerring.datasource.DataSourceContextHolder;
import com.elink.enginerring.datasource.DataSourceType;
import com.elink.enginerring.file.service.ITypeService;
import com.elink.enginerring.type.entity.Type;
import com.elink.enginerring.util.JSONUtil;

@RestController
@RequestMapping("/type")
public class FileTypeController {
	@Autowired
	private ITypeService typeService;

	/**
	 * 获取文件格式
	 * @return
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public Map<String, Object> getList() {
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return typeService.getList();
	}

	
	/**
	 * 添加文件格式
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Map<String, Object> add(@ModelAttribute @Valid Type type) {
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		typeService.add(type);
		return JSONUtil.getJSON(ErrorType.SUCCESS) ;
	}
	
	/**
	 * 修改文件格式
	 * @param type
	 * @return
	 */
	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	public Map<String, Object> modify(@ModelAttribute @Valid Type type) {
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		typeService.modify(type);
		return JSONUtil.getJSON(ErrorType.SUCCESS) ;
	}
	
	/**
	 * 删除文件格式
	 * @param typeId
	 * @return
	 */
	@RequestMapping(value = "/del", method = RequestMethod.GET)
	public Map<String, Object> del(@RequestParam(value="ids",required=true) String ids) {
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		typeService.del(ids);
		return JSONUtil.getJSON(ErrorType.SUCCESS) ;
	}
	
	/**
	 * 挤压式排序
	 * @param id_seq
	 * @return
	 */
	@RequestMapping(value="/order",method=RequestMethod.GET)
	public Map<String, Object> order(@RequestParam(value="id_seq",required=true) String id_seq){
		DataSourceContextHolder.setDbType(DataSourceType.DBDATABASE);
		System.out.println("选择的数据库" + DataSourceContextHolder.getDbType());
		return typeService.order(id_seq);
	}
}
